package com.example.gamestore.service.Impl;

import com.example.gamestore.mapper.UserMapper;
import com.example.gamestore.pojo.User;
import com.example.gamestore.service.UserService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author zihao
 * @date 2025/6/6 9:08
 * @VERSION
 * @description
 **/
@Service
public class UserServiceImpl implements UserService {
    @Resource
    private UserMapper userMapper;



    @Override
    public User login(User user) {
        try {
            System.out.println("进入到userService");
            User user1 = userMapper.login(user.getUsername());
            if(user1 != null){
                if(user1.getPassword().equals(user.getPassword())){
                    return  user1;
                }
            }
            return null;
        } catch (Exception e) {
            // 记录日志
            e.printStackTrace();
            return null;
        }
    }

    @Override
    public List<User> query() {
        System.out.println("进入userservice");
        return userMapper.query();
    }

    @Override
    public void updateUser(User user) {
        userMapper.updateUser(user);
    }


    @Override
    public User add(User user) {
        userMapper.insert(user);
        return user;
    }

    @Override
    public void delete(Long id) {
        userMapper.delete(id);
    }

    @Override
    public User getUserByUsernameAndPassword(String username, String password) {
        return userMapper.getUserByUsernameAndPassword(username, password);
    }

    @Override
    public boolean updatePassword(Long userId, String oldPassword, String newPassword) {
        User user = userMapper.getUserById(userId);
        if (user != null && user.getPassword().equals(oldPassword)) {
            user.setPassword(newPassword);
            int result = userMapper.updatePassword(user);
            return result > 0;
        }
        return false;
    }

    @Override
    public User getUserById(Long id) {
        return userMapper.getUserById(id);
    }


}
